package com.bestcem.xm.ticket.dao.impl.mysql;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.ticket.dao.AlertRecordDao;
import com.bestcem.xm.ticket.dao.converter.RecordConverter;
import com.bestcem.xm.ticket.dao.mapper.RecordDaoMapper;
import com.bestcem.xm.ticket.entity.mongo.Record;
import com.bestcem.xm.ticket.entity.mysql.RecordDO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/17 18:56
 */
@Repository
public class AlertRecordDaoImpl implements AlertRecordDao {

    @Resource
    private RecordDaoMapper recordDaoMapper;

    @Override
    public Record saveTicketRecord(Record entity) {
        RecordDO entityDo = RecordConverter.dtoToDo(entity);
        if (Objects.isNull(entityDo)) {
            return null;
        }

        if (Objects.isNull(entityDo.getRecordId())) {
            entityDo.setRecordId(ObjectId.next());
        }

        if (Objects.isNull(entityDo.getCreateTime())) {
            entityDo.setCreateTime(DateUtil.getCommonDate());
        }
        if (Objects.isNull(entityDo.getUpdateTime())) {
            entityDo.setUpdateTime(DateUtil.getCommonDate());
        }

        recordDaoMapper.insert(entityDo);
        return RecordConverter.doToDto(entityDo);
    }

    @Override
    public Record findById(String recordId) {
        RecordDO recordDO = recordDaoMapper.findByRecordId(recordId);

        return RecordConverter.doToDto(recordDO);
    }

    /**
     * 根据问卷id查询记录列表
     *
     * @param projectId 问卷id
     * @return List<Record>
     * @author gy.liu
     * @date 2021/10/22
     */
    @Override
    public List<Record> getRecordListByProjectId(String projectId) {
        // 220114时, 没人使用这个方法, 就不写实现了
        return null;
    }
}
